package com.longbig.multifunction.helper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class FileHelper {

    /**
     * 读取文件后以数组形式存放
     *
     * @param inputFile
     * @return
     * @throws Exception
     */
    public static List<String> readFileToStringList(String inputFile) {
        List<String> stringList = Lists.newArrayList();
        try {
            FileReader fileReader = new FileReader(inputFile);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String line = bufferedReader.readLine();
            while (line != null) {
                stringList.add(line);
                line = bufferedReader.readLine();
            }
            fileReader.close();
        } catch (IOException e) {
            log.error("读取输入文件失败：{}", inputFile);
            e.printStackTrace();
        }
        return stringList;
    }

    public static JSONArray readFileToString(String filepath) throws IOException {
//        File file = new File();
        File file = new File(filepath);

        String file1 = FileUtils.readFileToString(file);//前面两行是读取文件

        JSONArray jsonArray = JSON.parseArray(file1);
        return jsonArray;

    }

    /**
     * 将Java对象保存为JSON格式的文件
     * @param object 要转换的Java对象
     * @param filePath 保存文件的路径
     */
    public static void saveObjectToJsonFile(Object object, String filePath) {
        // 使用FastJSON将对象转换为JSON字符串
        String jsonStr = JSON.toJSONString(object);

        // 将JSON字符串写入文件
        try (FileWriter writer = new FileWriter(filePath)) {
            writer.write(jsonStr);
        } catch (IOException e) {
            e.printStackTrace();
            // 可以选择抛出运行时异常，或者进行其他的错误处理
            throw new RuntimeException("保存JSON文件时发生错误", e);
        }
    }

    /**
     * 将Java对象保存为JSON格式的文件
     * @param jsonStr 要转换的Java对象
     * @param filePath 保存文件的路径
     */
    public static void saveStringToFile(String jsonStr, String filePath) {

        // 将JSON字符串写入文件
        try (FileWriter writer = new FileWriter(filePath)) {
            writer.write(jsonStr);
        } catch (IOException e) {
            e.printStackTrace();
            // 可以选择抛出运行时异常，或者进行其他的错误处理
            throw new RuntimeException("保存JSON文件时发生错误", e);
        }
    }

    /**
     * 创建一个单一目录。
     * @param directoryPath 目录的路径
     * @return 成功或失败
     */
    public static boolean createSingleDirectory(String directoryPath) {
        File directory = new File(directoryPath);
        if (!directory.exists()) {
            return directory.mkdir();
        }
        return false;
    }

    public static void deleteFolder(String folderPath) {
        File directory = new File(folderPath);
        delete(directory);
    }

    public static void delete(File file) {
        if (file.isDirectory()) {
            for (File subFile : file.listFiles()) {
                delete(subFile);
            }
        }
        file.delete();
    }

    public static void writeFile(String filePath, String msg) {
        FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        try {
            File file = new File(filePath);
            if (!file.getParentFile().exists())
                file.getParentFile().mkdirs();
            fos = new FileOutputStream(file, true);
            osw = new OutputStreamWriter(fos, "UTF-8");
            osw.write(msg + "\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (osw != null)
                    osw.close();
                if (fos != null)
                    fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void writeFileWithStar(String filePath, String msg) {
        writeFile(filePath, msg + "\n***********************************************");
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            writeFileWithStar("D:\\移动工作\\共享接口方案\\测试环境测试\\采集结果文件\\错误报告测试.txt", i + "test");
        }
    }

}
